package asia.kalin.utils;

import asia.kalin.config.ApiKeyConfig;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.zhipu.oapi.ClientV4;
import com.zhipu.oapi.Constants;
import com.zhipu.oapi.service.v4.model.ChatCompletionRequest;
import com.zhipu.oapi.service.v4.model.ChatMessage;
import com.zhipu.oapi.service.v4.model.ChatMessageRole;
import com.zhipu.oapi.service.v4.model.ModelApiResponse;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Author: Kalin
 * @Name：ZhiPuUtil
 * @Date: 2025/3/20
 * @description
 */
public class DeepSeekJanusPro7B2Util {
    private static final String API_SECRET_KEY = ApiKeyConfig.getValue();


    private static final ClientV4 client = new ClientV4.Builder(API_SECRET_KEY)
            .enableTokenCache()
            .networkConfig(300, 100, 100, 100, TimeUnit.SECONDS)
            .connectionPool(new okhttp3.ConnectionPool(8, 1, TimeUnit.SECONDS))
            .build();

    // 请自定义自己的业务id
    private static final String requestIdTemplate = "mycompany-%d";

    private static final ObjectMapper mapper = new ObjectMapper();


    public static ObjectMapper defaultObjectMapper() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
        return mapper;
    }


    public static ModelApiResponse generatesDetectionResultsByAI(String prompt,String imgBase64) {
        // 采用List容器装消息及图片数据
        List<ChatMessage> chatMessageList = new ArrayList<>();
        // 创建第一个内容项 - 图片URL
        List<Map<String,Object>> contents = new ArrayList<>();
        Map<String,Object> imgMap = new HashMap<>(){{
            put("image_url",new HashMap<>(){{
                put("url",imgBase64);
            }});
            put("type","image_url");
        }};
        // 用户提问
        Map<String,Object> userQuestion = new HashMap<>(){{
            put("text",prompt);
            put("type","text");
        }};
        contents.add(imgMap);
        contents.add(userQuestion);
        ChatMessage chatMessage = new ChatMessage(ChatMessageRole.USER.value(), contents);
        chatMessageList.add(chatMessage);


        // 得到requestId
        String requestId = String.format(requestIdTemplate, System.currentTimeMillis());


        ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
                .model("glm-4v-plus-0111")
                .stream(Boolean.FALSE)
                .invokeMethod(Constants.invokeMethod)
                .messages(chatMessageList)
                .requestId(requestId)
                .build();
        // 调用模型
        ModelApiResponse invokeModelApiResp = client.invokeModelApi(chatCompletionRequest);
        return invokeModelApiResp;
    }
}
